package com.server.chart;

import com.dao.chart.ShowChartsMapper;
import com.model.bo.TimeReture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
处理时间分段
 */
@Service
public class TimeConverterUtil {
    @Autowired
    ShowChartsMapper showChartsMapper;

    //x轴时间类型
    public TimeReture timeConverter(String xColumnName, String tableName, String timeType, String screen, int counts){
        TimeReture timeReture = new TimeReture();
        String timeGroup = "'YYYY-MM-DD'";
        List<String> value = new ArrayList<String>();
        if ("year".equals(timeType)){
            timeGroup = "'YYYY'";
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("month".equals(timeType)){
            timeGroup = "'YYYY-MM'";
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("day".equals(timeType) || "".equals(timeType)){
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("quarter".equals(timeType)){
            timeGroup = "'YYYY-Q'";
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("week".equals(timeType)){
            timeGroup = "'YYYY-WW'";
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("halfYear".equals(timeType)){
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeYear(xColumnName, tableName, screen);
            timeReture.setValues(value);
        }else {
            timeGroup = "'YYYY-MM-DD'";
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen,counts);
            timeReture.setValues(value);
        }
        return timeReture;
    }

    //x轴时间类型包装
    public List<String> toRespons(List<String> dataList, String timeType){
        List<String> value = new ArrayList<String>();
        String dataLast = null;
        Iterator isDataList = dataList.iterator();
        while (isDataList.hasNext()){
            String data = (String) isDataList.next();
            if (data == null || "".equals(data)){
                continue;
            }
            if ("quarter".equals(timeType)){
                String[] str = data.split("-");
                dataLast = str[0] + "-Q" + str[1];
            }else if ("month".equals(timeType)){
                String[] str = data.split("-");
                dataLast = str[0] + "-M" + str[1];
            }else if ("week".equals(timeType)){
                String[] str = data.split("-");
                dataLast = str[0] + "-W" + str[1];
            }
            value.add(dataLast);
        }
        return value;
    }

    //无分组时间值
    public String getTimeValueConverter(String yColumn, String xColumn, String tableName, String dataType, String timeType, String xdata, String screen){

        String timeGroup = "'YYYY-MM-DD'";
        String value = null;
        //判断时间类型
        if (xdata == null || "".equals(xdata)){
            value = showChartsMapper.getTimeIsNull(dataType, yColumn, tableName, xColumn, screen);
        }else {
            if ("halfYear".equals(timeType)) {
                String[] str = xdata.split("-");
                if ("H1".equals(str[1])) {
                    String firstIndex = str[0] + "-01";
                    String afterIndex = str[0] + "-06";
                    value = showChartsMapper.getTimeValueYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, screen);
                } else {
                    String firstIndex = str[0] + "-07";
                    String afterIndex = str[0] + "-12";
                    value = showChartsMapper.getTimeValueYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, screen);
                }
            } else if ("year".equals(timeType)) {
                timeGroup = "'YYYY'";
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else if ("month".equals(timeType)) {
                timeGroup = "'YYYY-MM'";
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else if ("day".equals(timeType) || "".equals(timeType)) {
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else if ("quarter".equals(timeType)) {
                timeGroup = "'YYYY-Q'";
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else if ("week".equals(timeType)) {
                timeGroup = "'YYYY-WW'";
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else {
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            }
        }
        return value;
    }

    //有分组时间类型值处理
    public String getGroupTimeConverter(String dataType, String yColumn, String tableName, String groupByName, String timeType,
                                        String groupsName, String xColumn, String xAxisDataOne, String screen){
        String timeGroup = "'YYYY-MM-DD'";
        String value = null;
        if(groupsName == null || "".equals(groupsName)){
            value = showChartsMapper.getGroupsValueTimeIsNull(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                    timeGroup, xAxisDataOne, screen);
        }else {
            if ("year".equals(timeType)) {
                timeGroup = "'YYYY'";
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("month".equals(timeType)) {
                timeGroup = "'YYYY-MM'";
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("quarter".equals(timeType)) {
                timeGroup = "'YYYY-Q'";
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("week".equals(timeType)) {
                timeGroup = "'YYYY-WW'";
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("day".equals(timeType) || "".equals(timeType)) {
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("halfYear".equals(timeType)) {
                String[] str = xAxisDataOne.split("-");
                if ("上半年".equals(str[1])) {
                    String firstIndex = str[0] + "-01";
                    String afterIndex = str[0] + "-06";
                    value = showChartsMapper.getTimeValueHalfYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, groupByName, groupsName, screen);
                } else {
                    String firstIndex = str[0] + "-07";
                    String afterIndex = str[0] + "-12";
                    value = showChartsMapper.getTimeValueHalfYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, groupByName, groupsName, screen);
                }
            } else {
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            }
        }
        return value;
    }
}
